package com.wcbdota.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.wcbdota.R;
import com.wcbdota.base.BaseMessage;
import com.wcbdota.base.BaseUi;
import com.wcbdota.base.Messages;

import com.wcbdota.base.C;
import com.wcbdota.list.ItemGridAdapterList;
import com.wcbdota.model.Items;
import com.wcbdota.util.UIUtil;

@SuppressWarnings("unchecked")
public class UiItems extends BaseUi{
	//yy
	private ArrayList<Items> itemlist =new ArrayList<Items>();
	
	public List<Items> itemdatanew = new ArrayList<Items>();
	//yyo
	
	private Button mBase,mBaseComsume,mBaseArms,mBaseAttribute,mBaseArcane,
	mCompose,mShengwu,mFaqi,mFangju,mWuqi,mFuzhu,mPutong,
	mMystery;
	
	private TextView 
	mTVconsume,mTVattribute,mTVarms,mTVarcane,
	mTVshengwu,mTVfaqi,mTVfangju,mTVfuzhu,mTVputong,mTVwuqi,
	pt_tv_titlename;
	
	private GridView gv_item;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.items_main);
		if(InitScream()==true){
			System.out.println(InitScream()+"");
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
		}
		UIUtil.updateFullscreenStatus(this, false);
		findView();
		this.doTaskAsync(C.task.itemList, C.api.itemList);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onStart() {
		super.onStart();
		useView();
		showBanner();
		showMenu();
	}

	@Override
	public void onTaskComplete(int taskId, BaseMessage message) {
		super.onTaskComplete(taskId, message);
		
		switch(taskId){
		case C.task.itemList:
			try{
				Messages.msg.msg_UiItems = message;
				itemlist =(ArrayList<Items>)message.getResultList("Items");
				
				itemdatanew =itemlist;
				gv_item=(GridView)findViewById(R.id.gv_item);
				//gv_item.setAdapter(new ItemGridAdapterList(this,itemdata,gv_item));
				getItemBySpecies("1");
				gv_item.setOnItemClickListener(new itemClickListener());
				
			}catch(Exception ex){
				ex.printStackTrace();
			}finally{
				
			}
			break;
		}
	}
	
	class itemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			
			Items items =(Items)itemdatanew.get(position);
			System.out.println(items);
			Intent itemIntent =new Intent(UiItems.this,UiItemContent.class);
			itemIntent.putExtra("item", items);
			startActivity(itemIntent);
			
//			LayoutInflater inflater =UiItems.this.getLayoutInflater();
//			View layout =inflater.inflate(R.layout.item_i_dialog,null);
			
			
//			TextView tv_dlg_summary =(TextView)layout.findViewById(R.id.tv_summary);
//			tv_dlg_summary.setText(itemdatanew.get(position).getSummary().toString());
//			
//			TextView tv_dlg_idesc=(TextView)layout.findViewById(R.id.tv_desc);
//			tv_dlg_idesc.setText(DataCheck.getData(itemdatanew.get(position).getDesc().toString()));
//			
//			TextView tv_dlg_price =(TextView)layout.findViewById(R.id.tv_price);
//			tv_dlg_price.setText(itemdatanew.get(position).getPrice().toString());
//			
//			TextView tv_dlg_eff =(TextView)layout.findViewById(R.id.tv_effect);
//			tv_dlg_eff.setText(DataCheck.toChangeLine(itemdatanew.get(position).getEffectarray().toString()));
//			
//			TextView tv_dlg_value=(TextView)layout.findViewById(R.id.tv_value);
//			tv_dlg_value.setText(DataCheck.toChangeLine(itemdatanew.get(position).getValuearray().toString()));
//			
//			ImageView img_dlg_ipic =(ImageView)layout.findViewById(R.id.img_iname);
//			String iurl =C.api.resbase+"/"+itemdatanew.get(position).getImg()+".png";
//			String cashkey =AppUtil.md5(iurl);
//			Bitmap bitmap =SDUtil.getImage(cashkey);
//			img_dlg_ipic.setImageBitmap(bitmap);
//			
//			new AlertDialog.Builder(UiItems.this).setTitle(itemdatanew.get(position).getName().toString())
//			.setView(layout).setPositiveButton("确定", null).show();
			
			
		}
		
	}
	
	
	
	//yy
	private void getItemBySpecies(String species){
		itemdatanew =new ArrayList<Items>();
		for(int i=0;i<itemlist.size();i++){
			if(itemlist.get(i).getSpecies().equals(species)){
				itemdatanew.add(itemlist.get(i));
			}
		}
		gv_item.setAdapter(new ItemGridAdapterList(this,itemdatanew,gv_item));
	}//yyo
	
	public void findView(){
		mBase=(Button) findViewById(R.id.bu_base);
		mCompose=(Button) findViewById(R.id.bu_compose);
		mMystery=(Button) findViewById(R.id.bu_mystery);
		mBaseComsume=(Button) findViewById(R.id.bu_base_consume);
		mBaseArms=(Button) findViewById(R.id.bu_base_arms);
		mBaseAttribute=(Button) findViewById(R.id.bu_base_attribute);
		mBaseArcane=(Button) findViewById(R.id.bu_base_arcane);
		
		mShengwu=(Button) findViewById(R.id.bu_shengwu);
		mFaqi=(Button) findViewById(R.id.bu_faqi);
		mFangju=(Button) findViewById(R.id.bu_fangju);
		mWuqi=(Button) findViewById(R.id.bu_wuqi);
		mFuzhu=(Button) findViewById(R.id.bu_fuzhu);
		mPutong=(Button) findViewById(R.id.bu_putong);
		
		mTVconsume=(TextView) findViewById(R.id.tv_base_consume);
		mTVattribute=(TextView) findViewById(R.id.tv_base_attribute);
		mTVarms=(TextView) findViewById(R.id.tv_base_arms);
		mTVarcane=(TextView) findViewById(R.id.tv_base_arcane);
		
		mTVshengwu=(TextView) findViewById(R.id.tv_shengwu);
		mTVfaqi=(TextView) findViewById(R.id.tv_faqi);
		mTVfangju=(TextView) findViewById(R.id.tv_fangju);
		mTVfuzhu=(TextView) findViewById(R.id.tv_fuzhu);
		mTVputong=(TextView) findViewById(R.id.tv_putong);
		mTVwuqi=(TextView) findViewById(R.id.tv_wuqi);
		findViewById(R.id.pt_bt_back).setOnClickListener(new ButtonListener());
		findViewById(R.id.pt_bt_reload).setOnClickListener(new ButtonListener());
		pt_tv_titlename = (TextView) findViewById(R.id.pt_tv_titlename);
		
	}
	
	public void useView(){
		
		mBase.setOnClickListener(new ButtonListener());
		mCompose.setOnClickListener(new ButtonListener());
		mMystery.setOnClickListener(new ButtonListener());
		mPutong.setOnClickListener(new ButtonListener());
		mFuzhu.setOnClickListener(new ButtonListener());
		mWuqi.setOnClickListener(new ButtonListener());
		mFaqi.setOnClickListener(new ButtonListener());
		mShengwu.setOnClickListener(new ButtonListener());
		mFangju.setOnClickListener(new ButtonListener());
		mBaseArcane.setOnClickListener(new ButtonListener());
		mBaseAttribute.setOnClickListener(new ButtonListener());
		mBaseArms.setOnClickListener(new ButtonListener());
		mBaseComsume.setOnClickListener(new ButtonListener());
		pt_tv_titlename.setText("装备");
	}
	
	class ButtonListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.pt_bt_back:
				finish();
				break;
			case R.id.pt_bt_reload:
				UiItems.this.doTaskAsync(C.task.itemList, C.api.itemList);
				break;
			case R.id.bu_base://基础物品
				setViewBase(View.VISIBLE);
				setViewCompose(View.GONE);
				buttonPress(R.drawable.wuping,R.drawable.wuping_un,R.drawable.wuping_un);
				basePress(R.drawable.base_un,R.drawable.base_un,R.drawable.base_un,R.drawable.consume);
				getItemBySpecies("1");

			break;//升级物品
			case R.id.bu_compose:
				setViewBase(View.GONE);
				setViewCompose(View.VISIBLE);
				buttonPress(R.drawable.wuping_un,R.drawable.wuping,R.drawable.wuping_un);
				composePress(R.drawable.fangju_un,R.drawable.fuzhu,R.drawable.fuzhu_un,
						R.drawable.normal_un,R.drawable.fuzhu_un,R.drawable.normal_un);
				getItemBySpecies("7");
			break;
			case R.id.bu_mystery://神秘商店
				setViewBase(View.GONE);
				setViewCompose(View.GONE);
				buttonPress(R.drawable.wuping_un,R.drawable.wuping_un,R.drawable.wuping);
				getItemBySpecies("11");
				break;
			case R.id.bu_base_consume://消耗
				basePress(R.drawable.base_un,R.drawable.base_un,R.drawable.base_un,R.drawable.consume);
				getItemBySpecies("1");
				break;
			case R.id.bu_base_arcane://奥术
				basePress(R.drawable.base,R.drawable.base_un,R.drawable.base_un,R.drawable.consume_un);
				getItemBySpecies("4");
				break;
			case R.id.bu_base_arms://军备
				basePress(R.drawable.base_un,R.drawable.base,R.drawable.base_un,R.drawable.consume_un);
				getItemBySpecies("3");
				break;
			case R.id.bu_base_attribute://属性
				basePress(R.drawable.base_un,R.drawable.base_un,R.drawable.base,R.drawable.consume_un);
				getItemBySpecies("2");
				break;
			case R.id.bu_fangju://防具
				composePress(R.drawable.fangju,R.drawable.fuzhu_un,R.drawable.fuzhu_un,
						R.drawable.normal_un,R.drawable.fuzhu_un,R.drawable.normal_un);
				getItemBySpecies("9");
				break;
			case R.id.bu_faqi://法器
				composePress(R.drawable.fangju_un,R.drawable.fuzhu,R.drawable.fuzhu_un,
						R.drawable.normal_un,R.drawable.fuzhu_un,R.drawable.normal_un);	
				getItemBySpecies("7");
				
				break;
			case R.id.bu_fuzhu://辅助
				composePress(R.drawable.fangju_un,R.drawable.fuzhu_un,R.drawable.fuzhu,
						R.drawable.normal_un,R.drawable.fuzhu_un,R.drawable.normal_un);	
				getItemBySpecies("6");
				break;
			case R.id.bu_putong://普通
				composePress(R.drawable.fangju_un,R.drawable.fuzhu_un,R.drawable.fuzhu_un,
						R.drawable.normal,R.drawable.fuzhu_un,R.drawable.normal_un);
				getItemBySpecies("5");
				break;
			case R.id.bu_shengwu://圣物
				composePress(R.drawable.fangju_un,R.drawable.fuzhu_un,R.drawable.fuzhu_un,
						R.drawable.normal_un,R.drawable.fuzhu,R.drawable.normal_un);
				getItemBySpecies("10");
				break;
			case R.id.bu_wuqi://武器
				composePress(R.drawable.fangju_un,R.drawable.fuzhu_un,R.drawable.fuzhu_un,
						R.drawable.normal_un,R.drawable.fuzhu_un,R.drawable.normal);
				getItemBySpecies("8");
				break;
			}
		}
		
	}
	
	public void composePress(int drawable1,int drawable2,int drawable3,int drawable4,int drawable5,int drawable6){
		mFangju.setBackgroundResource(drawable1);
		mFaqi.setBackgroundResource(drawable2);
		mFuzhu.setBackgroundResource(drawable3);
		mPutong.setBackgroundResource(drawable4);
		mShengwu.setBackgroundResource(drawable5);
		mWuqi.setBackgroundResource(drawable6);
		
	}
	
	public void basePress(int drawable1,int drawable2,int drawable3,int drawable4){
		mBaseArcane.setBackgroundResource(drawable1);
		mBaseArms.setBackgroundResource(drawable2);
		mBaseAttribute.setBackgroundResource(drawable3);
		mBaseComsume.setBackgroundResource(drawable4);
	}
	
	public void buttonPress(int drawable1,int drawable2,int drawable3){
		mBase.setBackgroundResource(drawable1);
		mCompose.setBackgroundResource(drawable2);
		mMystery.setBackgroundResource(drawable3);
	}
	
	public void setViewBase(int v){
		mBaseComsume.setVisibility(v);
		mBaseArms.setVisibility(v);
		mBaseAttribute.setVisibility(v);
		mBaseArcane.setVisibility(v);
		mTVconsume.setVisibility(v);
		mTVattribute.setVisibility(v);
		mTVarms.setVisibility(v);
		mTVarcane.setVisibility(v);
		
	}
	
	public void setViewCompose(int v){
		mShengwu.setVisibility(v);
		mFaqi.setVisibility(v);
		mFangju.setVisibility(v);
		mWuqi.setVisibility(v);
		mFuzhu.setVisibility(v);
		mPutong.setVisibility(v);
		mTVshengwu.setVisibility(v);
		mTVfaqi.setVisibility(v);
		mTVfangju.setVisibility(v);
		mTVfuzhu.setVisibility(v);
		mTVputong.setVisibility(v);
		mTVwuqi.setVisibility(v);
	}
	
}
